﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using org.project.web.utils;

public partial class nDetail : System.Web.UI.Page
{
    string sqlConnString = "Data Source=asdf80730.no-ip.org;Initial Catalog=project;Persist Security Info=True;User ID=sa;Password=P@ssword";//設定資料庫名稱與路徑
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection cnn = new SqlConnection(sqlConnString);//建立Connection 物件
        int NId = Convert.ToInt32(Request.QueryString["NId"]);
        int SMId = Convert.ToInt32(Session["MId"]);
        
        cnn.Open();//使用Open開啟資料庫

        SqlCommand cmd1 = new SqlCommand("SELECT Negotiation.NId,Wanted.MId FROM Negotiation,Wanted WHERE (Negotiation.WId=Wanted.WId) AND Negotiation.NId=" + NId, cnn);
        SqlCommand cmd2 = new SqlCommand("SELECT COUNT(Negotiation.NId) FROM Negotiation,DealHistory WHERE (Negotiation.NId=DealHistory.NId) AND (DealHistory.NId=" + NId + "AND " + "HCId=2)", cnn);
        SqlDataReader sqlreader1,sqlreader2;

        sqlreader1 = cmd1.ExecuteReader();
      

		

        if (sqlreader1.Read())
        {
            int MId = sqlreader1.GetSqlInt32(1).Value;
           // Response.Write("<script language=JavaScript>alert('" +"Session="+SMId+ ",tagetNId=" + Convert.ToInt32(Request.QueryString["NId"]) +",MId="+ sqlreader.GetSqlInt32(1).Value.ToString() + "')</script>");
            if(MId==SMId)
			btnDeal.Visible = true;
			else
			btnDeal.Visible = false;
        }
        else
        {
		btnDeal.Visible = true;
            Response.Write("<script language=JavaScript>alert('sqlreader false')</script>");
        }
        cnn.Close();

        cnn.Open();//使用Open開啟資料庫
        sqlreader2 = cmd2.ExecuteReader();
        if (sqlreader2.Read())
        {
            int HCIdCount = sqlreader2.GetSqlInt32(0).Value;
            // Response.Write("<script language=JavaScript>alert('" +"Session="+SMId+ ",tagetNId=" + Convert.ToInt32(Request.QueryString["NId"]) +",MId="+ sqlreader.GetSqlInt32(1).Value.ToString() + "')</script>");
            if (HCIdCount == 0)
                btnDeal.Visible = true;
            else
                btnDeal.Visible = false;
        }
        else
        {
            btnDeal.Visible = true;
            Response.Write("<script language=JavaScript>alert('sqlreader false')</script>");
        }
        cnn.Close();
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["MId"] == null)//會員
            this.Page.MasterPageFile = "~/MasterPage.master";
        else       //一般使用者
            this.Page.MasterPageFile = "~/LoginedMasterPage.master";
    }
    protected void btnDeal_Click(object sender, EventArgs e)
    {
        String WId = (DetailsView1.Rows[0].Cells[1].Text);
        String NId = (DetailsView1.Rows[1].Cells[1].Text);
        
        //交易完成
        int HCId = 2;
        int MId = Convert.ToInt32(Session["MId"]);
        String DHTimestamp = DateTime.Now.ToShortDateString();
        String InsertDealHistorySql = "INSERT INTO DealHistory(WId, NId, HCId, DHTimestamp, MId) VALUES('" 
                                                             + WId + "','" +NId + "','"+HCId +"','" + DHTimestamp + "','" + MId +"')";

        SQLUtility utility = new SQLUtility();
        int insertResult = utility.ExecuteCommandNonQuery(sqlConnString, InsertDealHistorySql);


        SQLUtility utility2 = new SQLUtility();
        String UpdateWantedSql = "UPDATE Wanted SET HCId ='" +HCId + "' FROM Wanted INNER JOIN Negotiation ON Wanted.WId = Negotiation.WId WHERE(Negotiation.NId ='"  + NId + "')";
        int updateResult = utility2.ExecuteCommandNonQuery(sqlConnString, UpdateWantedSql);

        Response.Write("<script >alert('交易完成！');</script>");
        Response.Redirect("myWented.aspx");
        
        /*
        Response.Write("WId=" + DetailsView1.Rows[0].Cells[1].Text);
        Response.Write("NId=" + DetailsView1.Rows[1].Cells[1].Text);
        */

    }
}